package com.ruoyi.project.hcy.task.service;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.hcy.administrations.domain.Crew;
import com.ruoyi.project.hcy.task.domain.Task;
import com.ruoyi.project.hcy.task.domain.TaskTransfer;
import com.ruoyi.project.hcy.task.mapper.TaskMapper;
import com.ruoyi.project.hcy.task.mapper.TaskTransferMapper;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 任务转移 服务层处理
 */
@Service
public class TaskTransferServiceImpl implements ITaskTransferService {

    @Autowired
    TaskTransferMapper taskTransferMapper;

    @Autowired
    TaskMapper taskMapper;

    @Override
    public List<TaskTransfer> selectTaskTransferList(TaskTransfer taskTransfer) {
        return taskTransferMapper.selectTaskTransferList(taskTransfer);
    }

    @Override
    public Crew selectCrewOfTask(Long taskId) {
        Task task = taskMapper.selectTaskById(taskId);
        Crew crew = task.getExecutorCrew();
        return crew;
    }

    @Override
    public int insertTransfer(TaskTransfer taskTransfer, Long taskId, Long userId) {
        Task task = taskMapper.selectTaskById(taskId);
        Crew user = taskMapper.selectCrewById(userId);
        taskTransfer.setUserCrew(user);
        taskTransfer.setTask(task);
        return taskTransferMapper.insertTransfer(taskTransfer);
    }

    @Override
    public List<Task> selectTaskList() {
        Task task = new Task();
        String loginName = ShiroUtils.getLoginName();
        User user = taskMapper.selectLoginUser(loginName);
        task.setCreatorUser(user);
        return taskMapper.selectTaskList(task);
    }

    @Override
    public int deleteTaskTransferByIds(String ids) {
        Long[] transferIds = Convert.toLongArray(ids);
        return taskTransferMapper.deleteTaskTransferByIds(transferIds);
    }

    @Override
    public TaskTransfer selectTaskTransferById(Long transferId) {
        return taskTransferMapper.selectTaskTransferById(transferId);
    }

    @Override
    public int updateTaskTransfer(TaskTransfer taskTransfer) {
        return taskTransferMapper.updateTaskTransfer(taskTransfer);
    }

    @Override
    public int verifyTaskTransfer(TaskTransfer taskTransfer, Long crewId, Long taskId) {
        String loginName = ShiroUtils.getLoginName();
        User user = taskMapper.selectLoginUser(loginName);
        taskTransfer.setVerifierUser(user);
        taskTransfer.setVerifyTime(new Date());
        if ("1".equals(taskTransfer.getTransferStatus())) {
            Crew executorCrew = taskMapper.selectCrewById(crewId);
            Task task = taskMapper.selectTaskById(taskId);
            task.setExecutorCrew(executorCrew);
            taskMapper.updateTask(task);
        }
        return taskTransferMapper.verifyTaskTransfer(taskTransfer);
    }

    @Override
    public List<Crew> selectCrews() {
        return taskTransferMapper.selectCrews();
    }
}
